package cc.chinagps.gateway;

import cc.chinagps.gateway.aplan.APlanServer;
import cc.chinagps.gateway.client.TransferDeviceManager;
import cc.chinagps.gateway.hbase.export.ExportHBase;
import cc.chinagps.gateway.log.LogManager;
import cc.chinagps.gateway.memcache.MemcacheUpdator;
import cc.chinagps.gateway.mq.CmdReader;
import cc.chinagps.gateway.mq.MQManager;
import cc.chinagps.gateway.mq.export.ExportMQInf;
import cc.chinagps.gateway.mq.export.ExportMQMult;
import cc.chinagps.gateway.mq.writer.MQWriter;
import cc.chinagps.gateway.seat.SeatServer;
import cc.chinagps.gateway.unit.UnitServer;
import cc.chinagps.gateway.unit.common.BaseStationManager;
import cc.chinagps.gateway.unit.eg.upload.DeviceInfoManager;
import cc.chinagps.gateway.util.SingletonFactory;
import cc.chinagps.gateway.util.SystemConfig;
import cc.chinagps.gateway.web.WebServer;

public class StartServer {

    public static StartServer instance = new StartServer();
    public static int isHistoryTime = 180;
    public static boolean isParseBaseStation = false;
    public static int KelongCanDataValidPeriod = 180000;
    public static int KelongBasestationDataValidPeriod = 180000;
    public static int obdNullVal = -999999;

    public static int deviceRefreshInterval = 180000;
    private UnitServer unitServer;

    public UnitServer getUnitServer() {
        return unitServer;
    }

    public void setUnitServer(UnitServer unitServer) {
        this.unitServer = unitServer;
    }


    public void start() {

        LogManager.init();

        int unitServerPort = Integer.valueOf(SystemConfig.getSystemProperty("unit_server_port"));

        int seatServerPort = Integer.valueOf(SystemConfig.getSystemProperty("seat_server_port"));
        int aPlanServerPort = Integer.valueOf(SystemConfig.getSystemProperty("a_plan_server_port"));

        int aPlanAlarmServerPort = Integer.valueOf(SystemConfig.getSystemProperty("a_plan_alarm_server_port"));

        String isParse = SystemConfig.getSystemProperty("parse_basestation");
        if (isParse != null) {
            isParseBaseStation = Boolean.valueOf(isParse);
        }

        unitServer = new UnitServer(unitServerPort);
        SeatServer seatServer = new SeatServer(seatServerPort);
        APlanServer aPlanServer = new APlanServer(aPlanServerPort, "APlan Server");
        APlanServer aPlanAlarmServer = new APlanServer(aPlanAlarmServerPort, "APlan Alarm Server");

        // seatServer -- unitServer
        unitServer.setSeatServer(seatServer);
        seatServer.setUnitServer(unitServer);


        // aPlanServer -- unitServer
        unitServer.setAPlanServer(aPlanServer);
        aPlanServer.setUnitServer(unitServer);


        // aPlanAlarmServer -- unitServer
        unitServer.setAPlanAlarmServer(aPlanAlarmServer);
        aPlanAlarmServer.setUnitServer(unitServer);


        // web
        WebServer webServer = new WebServer();
        webServer.setUnitServer(unitServer);

        try {
            String obdNullValStr = SystemConfig.getSystemProperty("obd_null_val");
            if (obdNullValStr != null) {
                obdNullVal = Integer.valueOf(obdNullValStr);
            }

            String historyTime = SystemConfig.getSystemProperty("is_history_time");
            if (historyTime != null) {
                isHistoryTime = Integer.valueOf(historyTime);
            }

            String canValidPeriod = SystemConfig.getSystemProperty("kelong_can_data_valid_period");
            if (canValidPeriod != null) {
                KelongCanDataValidPeriod = Integer.valueOf(canValidPeriod);
            }

            String basestationValidPeriod = SystemConfig.getSystemProperty("kelong_basestation_data_valid_period");
            if (basestationValidPeriod != null) {
                KelongBasestationDataValidPeriod = Integer.valueOf(basestationValidPeriod);
            }

            String deviceRefreshIntervalStr = SystemConfig.getSystemProperty("device_refresh_interval");
            if (deviceRefreshIntervalStr != null) {
                deviceRefreshInterval = Integer.valueOf(basestationValidPeriod);
            }

            // init hbase
            boolean hbaseEnabled = Boolean.valueOf(SystemConfig.getHBaseProperty("enabled"));
            ExportHBase exportHBase = new ExportHBase();
            exportHBase.setEnabled(hbaseEnabled);
            unitServer.setExportHBase(exportHBase);

            if (hbaseEnabled) {
                exportHBase.init();
            } else {
                System.err.println("HBase is disabled!");
            }

            if (isParseBaseStation) {
                BaseStationManager.getInstance().init();
            }
            // init memcache
            // MemcacheManager.init();
            MemcacheUpdator memcacheUpdator = new MemcacheUpdator();
            memcacheUpdator.setUnitServer(unitServer);
            unitServer.setMemcacheUpdator(memcacheUpdator);


            memcacheUpdator.startWork();

            // init mq
            boolean mqEnabled = Boolean.valueOf(SystemConfig.getMQProperty("enabled"));

            // ExportMQInf exportMQ = new ExportMQSingle();
            ExportMQInf exportMQ = new ExportMQMult();
            exportMQ.setEnabled(mqEnabled);
            unitServer.setExportMQ(exportMQ);


            if (mqEnabled) {
                MQManager.init();

                // write cmd-response to mq
                exportMQ.init();
                exportMQ.startWorker();

                MQWriter.getInstance().init();

                CmdReader reader = new CmdReader(exportMQ, unitServer);
                unitServer.setCmdReader(reader);

                // read cmd from mq
                reader.init();
                reader.startWorker();
            } else {
                System.err.println("ActiveMQ is disabled!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        unitServer.startService();
        seatServer.startService();
        aPlanServer.startService();
        aPlanAlarmServer.startService();
        try {
            webServer.startService();
        } catch (Exception e) {
            e.printStackTrace();
        }

        SingletonFactory.getInstance(DeviceInfoManager.class).startService();
        TransferDeviceManager.instance.init();
        System.out.println("system ready!");
    }

    public static void main(String[] args) {
        instance.start();
    }
}